package yanyan.com.tochar.funsactivity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import com.gs.buluo.common.widget.LoadingDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import androidx.annotation.Nullable;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import yanyan.com.tochar.FunsHiddenImgActivity;
import yanyan.com.tochar.FunsWebAppActivity;
import yanyan.com.tochar.R;
import yanyan.com.tochar.beans.WebAppInfo;
import yanyan.com.tochar.utils.CoreUtil;
import yanyan.com.tochar.utils.DialogUtil;
import yanyan.com.tochar.utils.EmptyUtils;
import yanyan.com.tochar.utils.StringUtil;
import yanyan.com.tochar.utils.ToastUtil;

/**
 * QQ长文字气泡
 */
public class FunsQqLongCharActivity extends Activity {
    private WebView webView;
    private EditText text,qpId;
    private boolean isCustom=false;
    private Spinner typeSp;
    private ArrayAdapter<String> typeAdapter;
    private Map<String,String> typeMap=new LinkedHashMap<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_funs_qq_longchar);
        text=findViewById(R.id.qq_long_char_text);
        typeSp=findViewById(R.id.qq_long_char_spnner);
        webView=findViewById(R.id.qq_long_char_web);
        qpId=findViewById(R.id.qq_long_char_id);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.supportMultipleWindows();  //多窗口
        settings.setAllowFileAccess(true);  //设置可以访问文件
        settings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        settings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
        //支持内容重新布局
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setAppCacheEnabled(true);//是否使用缓存
//        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        settings.setDomStorageEnabled(true);//DOM Storage
        settings.setLoadWithOverviewMode(true);//和setUseWideViewPort(true)一起解决网页自适应问题
        settings.setDomStorageEnabled(true);//主要是这句

        //统计各功能
        CoreUtil.statisticalFuns(this,"funs_qq__long_font");


        typeMap.put("纯白蕾丝","2271");
        typeMap.put("环游太空","2551");
        typeMap.put("萌系虫虫","2514");
        typeMap.put("折纸","2516");
        typeMap.put("热气球","2493");
        typeMap.put("忍者","2494");
        typeMap.put("香甜西瓜","2464");
        typeMap.put("小小动物","2465");
        typeMap.put("火龙果","2428");
        typeMap.put("海盗船","2427");
        typeMap.put("传送门","2426");
        typeMap.put("浪漫星云","2351");
        typeMap.put("冰淇淋","2319");
        typeMap.put("可爱小动物","2320");
        typeMap.put("紫色梦幻","2321");
        typeMap.put("简约金属","2232");
        typeMap.put("梦幻之红","2239");
        typeMap.put("清新绿色","2240");
        typeMap.put("雨蛙呱呱","2276");
        typeMap.put("颜文字","2275");
        typeMap.put("天使之翼","2274");
        typeMap.put("黑色蕾丝","2273");
        typeMap.put("恶魔之翼","2272");
        typeMap.put("自定义id","0000");
        List<String> typeList=new ArrayList();
        //将key转list
        Iterator<String> iterator = typeMap.keySet().iterator();
        while (iterator.hasNext()){
            typeList.add(iterator.next().toString());
        }
        typeAdapter=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,typeList);
        typeAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
        typeSp.setAdapter(typeAdapter);

        typeSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position==typeList.size()-1){
                    isCustom=true;
                    qpId.setVisibility(View.VISIBLE);
                }else {
                    qpId.setVisibility(View.GONE);
                    isCustom=false;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    public void openQqSetting(View view){
        String textStr=text.getText().toString();
        if(EmptyUtils.isEmpty(textStr)){
            ToastUtil.showLongToast(this,"请输入文字");
            return;
        }
        String id;
        if(isCustom){
            id=qpId.getText().toString();
            if(EmptyUtils.isEmpty(id)){
                ToastUtil.showLongToast(this,"请输入气泡id");
                return;
            }
        }else {
            String type=typeSp.getSelectedItem().toString();
            id=typeMap.get(type);
        }
        LoadingDialog.getInstance().show(FunsQqLongCharActivity.this, "正在生成", false);//加载
        beginCreate(textStr,id);


    }


    private void beginCreate(String textStr,String id){
        String url="https://zb.vip.qq.com/collection/aio?diyText=";
        url=url+textStr+"&_wv=16778243&id=";
        url=url+id+"&adtag=mvip.gongneng.android.bubble.collection_aio&_preload=1&type=bubble&_wvx=3&adtag=mvip.gongneng.android.bubble.index_dynamic_tab";
        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //这里一定要是url  不能是网页
                view.loadUrl(url);
                return true;
            }

            @Nullable
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, String  url) {
                if (url.startsWith("http") || url.startsWith("https")) { //http和https协议开头的执行正常的流程
                    return super.shouldInterceptRequest(view, url);
                } else { //其他的URL则会开启一个Acitity然后去调用原生APP
                    Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(in);
                    LoadingDialog.getInstance().dismissDialog();
                    return null;
                }
            }
        });

    }
}
